<?php 
require_once('../connections/con_gl.php');
$cabang = $_GET['cabang'];
$bulan = $_GET['bulan'];
$tahun = $_GET['tahun'];
global $num,$days,$first,$last;
$num =cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);
$first =  $tahun.'/'.$bulan.'/'.'01';
$last =  $tahun.'/'.$bulan.'/'.$num;

list($namacabang) = mysql_fetch_row(mysql_query("select gudang from gl_gudang where id='$cabang'"));
?>

<h3>STATUS KENDARAAN UNIT TLI <?php echo strtoupper($namaarea); ?></h3>
<table width="100%" border="1" cellspacing="0" cellpadding="3">	
	<tr style="text-align:center" bgcolor="#7d7d7d" >
		<th width="3%" style="color:rgb(255,255,255)">No.</th>
		<th width="7%" style="color:rgb(255,255,255)">UNIT</th>
		
		<?php
		
	
		$days = array();
		for($i=1; $i<=$num; $i++)
		{
			echo "<th style='color:rgb(255,255,255)'>$i</th>";
		
		}
		
				
		?>
		<th width="7%" style="color:rgb(255,255,255)">CASHIN</th>
		<th width="7%" style="color:rgb(255,255,255)">CASHOUT</th>
		<th width="7%" style="color:rgb(255,255,255)">PPN</th>
	</tr>
	<?php
	
	global $datesArray,$query2,$arr;
	
	unset($datesArray); 	
	$datesArray[] = array();
	$arr[] = array();
	
	
	$sql = "SELECT a.kendaraan,a.booking_date FROM gl_book as a inner join gl_retailtrans as b on (a.no_ref = b.no_ref) WHERE a.booking_date BETWEEN '$first' AND '$last' ";
	$q = mysql_query($sql) or die (mysql_error());
			
	while($result = mysql_fetch_array($q)){
		
		$arrNoPol[] = $result['kendaraan'];
		$arrBookDate[] = $result['booking_date'];
			
	}	
	//Print_r($arrNoPol);
	//Print_r($arrBookDate);
	
	$query3 = mysql_query("SELECT gl_book.kendaraan, gl_book.booking_date, gl_retailtrans.supir, gl_retailtrans.bbm, gl_retailtrans.tol, gl_retailtrans.parkir, gl_retailtrans.akomod, gl_detailtrans.hargasatuan, gl_detailtrans.ppn
						   FROM gl_retailtrans, gl_book, gl_detailtrans WHERE gl_book.no_ref = gl_retailtrans.no_ref AND gl_detailtrans.no_ref = gl_book.no_ref AND gl_book.booking_date BETWEEN '$first' AND '$last' ") or die (mysql_error());
	while($hasil = mysql_fetch_array($query3)){
		$arrKendaraan[] = $hasil['kendaraan'];		
		$cashin[] = $hasil['hargasatuan'];
		$cashout[] = $hasil['supir']+$hasil['bbm']+$hasil['tol']+$hasil['parkir']+$hasil['akomod'];
		$ppn[] = $hasil['ppn'];
		$booking[] = $hasil['booking_date'];
	}
	//Print_r($booking);
	
	// disini bermain nya
			
		//D 1344 QE
		 
		 global $q, $s;
		 $s = 0;
		 $ketemu = false;
		 $no = 1;
		 $query2 = mysql_query("SELECT DISTINCT a.nopolisi,a.tipekendaraan FROM gl_masterkendaraan as a inner join gl_tipekendaraan as b on (a.tipekendaraan = b.id) where a.cabang = $cabang") or die (mysql_error());
		  while(list($nopolisi,$tipekend) = mysql_fetch_row($query2)){
		  	//Print_r($nopolisi);
			echo "<tr>";
			echo "<td align='center'>$no.</td>";
			echo "<td align='left'>$nopolisi</td>";
			
			//echo $num;
			for($i=1; $i<=$num; $i++)
			{
			   
			   if($i<=9)
			   {
					$days = '0'.$i;
			   }
			   else
			   {
				   $days = $i;
			   }
				
				$value = $tahun.'-'.$bulan.'-'.$days;
				//print_r($value);
				 
				
				
				if( searchday_in_array($nopolisi,$value,$arrBookDate,$arrNoPol) == 1)
				{
						$s++;
						echo "<td align='center' bgcolor='#d25a5a' style='color:rgb(255,255,255)'><span>1</span></td>";
					
				}else{
						
						echo "<td align ='center'><span>0</span></td>";
						
				}
				
				
				
					
			}
			
				//print_r($s);
				
				$d = searchCash($nopolisi,$arrKendaraan,$arrBookDate,$booking);
				
				if ($d[0] == 1)
				{	
					
					$t = $d[1];
					$total = $cashin[$t]*$s;
				?>
					<td align='center' bgcolor='#d25a5a' style='color:rgb(255,255,255)'><span><?php echo number_format($total,0,',','.').",-"; ?></span></td>
					<td align='center' bgcolor='#d25a5a' style='color:rgb(255,255,255)'><span><?php echo number_format($cashout[$t],0,',','.').",-"; ?></span></td>
					<td align='center' bgcolor='#d25a5a' style='color:rgb(255,255,255)'><span><?php echo number_format($ppn[$t],0,',','.').",-"; ?></span></td>
				
				<?php	
				}else{
					echo "<td align ='center'><span>0,-</span></td>";
					echo "<td align ='center'><span>0,-</span></td>";
					echo "<td align ='center'><span>0,-</span></td>";
				}
				
			  $s = 0;
			  $no++;
		  }
		  
		  
		  
			function searchCash($nopolisi,$arrKendaraan,$arrBookDate,$booking){
			
			  $a = 0;
			  $b = 0;
			  $c = 0;
			  
			  
			  for ($i = 0; $i < count($arrKendaraan);$i++) {    
				for($y = 0; $y < count($booking);$y++){
				 if( $arrKendaraan[$i] == $nopolisi && $booking[$y] == $arrBookDate[$c] ) {
					$t = $i;
				    $a = 1;
					$c++;
					
					
				 } 
				}
				
			  }
			  
			  
			  
			  			  
			  return array($a,$t);
			}
		  
		  
			function searchday_in_array($nopolisi,$value, $arrBookDate,$arrNoPol){
			  
			  
			  $a = 0;
			  
			  for ($i = 0; $i < count($arrBookDate);$i++) {    
				  if($arrBookDate[$i] == $value && $arrNoPol[$i] == $nopolisi) {
					
				    $a=1;
				  } 
				 
			  }
			  return $a;
			}
			
			
			
	?>
		
		
	</tr>
	
	
</table>